import * as reportPlanService from '../../services/report/reportPlan';
import {queryAssetType} from '../../services/assetType';
import {queryAssetBrand,queryAssetModelAll} from '../../services/assetModel';
import {queryAllRoomList} from '../../services/tRoom';
import {qryRackList} from '../../services/equipment';
import {qryDepts,qryUserByDept} from '../../services/user';
import moment from 'moment'

export default {
  namespace: 'reportPlan',
  state: {
    list: [], 
    current : 1,
    currentSize : 10,
    total : 0, 
    newKey :'',
    modalType : 'create',    
    currentItem: {}, 
    assetTypeTree:[],
    roomList:[],
    assetBrandList:[],
    assetModelList:[],
    rackList:[],
    manageDeptTree:[],
    useDeptTree:[],
    userList:[],
    isShow:false,
    typeId:undefined,
    brandId:undefined,
    cycletime:{
      cycleType: 1,
      reportPlanList:[],
    },
    condition:{},
    viewModalVisible:false,    
    detailList:[],
    detailCurrent:1,
    detailCurrentSize:10,
    detailTotal:0,
    viewModalType:'view',
    currentDetailItem:{},
    viewItemModalVisible:false,
    viewItem:{},
    modalViewType:'view',
    obj:{},
    isShowTime:2,
  },
  reducers: {    
    upState(state, { payload}) {
      return { ...state, ...payload };
    },
    showDetail(state, { payload}) {
      return { ...state, ...payload };
    },
  },
  effects: {       
    *queryList({ payload },{ call, put }) {
      const { body } = yield call(reportPlanService.queryList,payload);
      yield put({ type: 'upState', payload: { list:body.data, current:body.current, currentSize:body.size, total:body.iTotalRecords}});
    },
    *queryAssetType({ payload },{ call, put }) {
      const { body }  = yield call(queryAssetType);
      yield put({ type: 'upState', payload: {assetTypeTree:body.data} });
    },
    *queryRoom({ payload },{ call, put }) {
      const { body } = yield call(queryAllRoomList,payload);
      yield put({ type: 'upState', payload: {roomList:body.data} });
    },
    *queryAssetBrand({ payload },{ call, put }) {
      const { body } = yield call(queryAssetBrand,payload);
      yield put({ type: 'upState', payload: {assetBrandList:body.data} });
    },
    *queryAssetModel({ payload },{ call, put }) {
      const { body } = yield call(queryAssetModelAll,payload);
      yield put({ type: 'upState', payload: {assetModelList:body.data,brandId:payload.brandId,typeId:payload.typeId} });
    },
    *qryRackList({ payload },{ call, put }) {
      const { body }  = yield call(qryRackList,payload);
      yield put({ type: 'upState', payload: {rackList:body.data}});
    },
    *qryManageDepts({ payload },{ call, put }) {    
      const { body }  = yield call(qryDepts);
      yield put({ type: 'upState', payload: {manageDeptTree :body.data} });
    },
    *qryUseDepts({ payload },{ call, put }) {    
      const { body }  = yield call(qryDepts);
      yield put({ type: 'upState', payload:{useDeptTree: body.data} });
    },
    *qryUserByDept({ payload },{ call, put }) {    
      const { body }  = yield call(qryUserByDept,payload);
      yield put({ type: 'upState', payload:{userList:body.data} });
    },
    *create({ payload },{ call, put }){
      const { body } = yield call(reportPlanService.create,payload);
       yield put({ type: 'upState',payload:{newKey : new Date().getTime() + '' , modalVisible: false, condition:{},}});
       yield put({ type: 'queryList' ,payload:{}});
    },
    *qryById({ payload, },{ call, put }){
      const { body } = yield call(reportPlanService.qryById,payload);
       if(body){        
        yield put({
          type: 'upState',
          payload: {
            viewItem: body.data,
            viewItemModalVisible: true,
            modalViewType: 'viewItem',
            title: '查看报表计划',
            obj: body.data.conditionVo,
            cycletime:{
              cycleType: body.data.cycleType,
              reportPlanList:body.data.reportPlanList,              
            },
          },
      });
     }
    }, 

    *remove({ payload ,search},{ call, put }){
      const { body } = yield call(reportPlanService.remove,payload);
      yield put({ type: 'queryList' ,payload:search});
    },  
    *onViewSheet({ payload ,name},{ call, put }){
      yield put({ type: 'showDetail' ,payload:{viewModalVisible: true,currentDetailItem:payload}});
      const { body } = yield call(reportPlanService.queryReportFileList,payload);      
      yield put({ type: 'upState',payload:{ detailList:body.data, detailCurrent:body.current, detailCurrentSize:body.size, detailTotal:body.iTotalRecords}});
    },
    *searchDetailList({ payload ,name},{ call, put }){
      const { body } = yield call(reportPlanService.queryReportFileList,payload);      
      yield put({ type: 'upState',payload:{ detailList:body.data, detailCurrent:body.current, detailCurrentSize:body.size, detailTotal:body.iTotalRecords}});
    },
    *removeFile({ payload },{ call, put }){
      const { body } = yield call(reportPlanService.removeFile,payload);
      yield put({ type: 'searchDetailList', payload});
    },  
    *downLoadFile({ payload },{ call, put }){
      const { body } = yield call(reportPlanService.downLoadFile,payload);
    },  
    *onCreateSheet({ payload },{ call, put }){
      const { body } = yield call(reportPlanService.onCreateSheet,payload.id);
    },  
   
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname }) => {
        if (pathname === '/reportPlan') {
          dispatch({ type: 'queryList', payload:{ 'name':'' }});
         
        }
      });
    },
  },
};
